home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / incoming / jstools-.6v3 / jstools- / jstools-tk3.6v3.0 / lib / jdoc_util.tcl < prev    next >
Encoding:
Text File  |  1995-02-09  |  6.7 KB  |  227 lines

  1. # jdoc_util.tcl - utility procedures for jdoc
  2. # Copyright 1992-1994 by Jay Sekora.  All rights reserved, except 
  3. # that this file may be freely redistributed in whole or in part 
  4. # for non¡profit, noncommercial use.
  5.  
  6. ##############################################################################
  7. # jdoc:init - basic initialisation
  8. ##############################################################################
  9.  
  10. proc jdoc:init {} {
  11.   global jstools_library
  12.   global tk_library
  13.   global J_PREFS            ;# general jstools user preferences
  14.   global JDOC_PREFS            ;# user preferences for jdoc
  15.   global JDOC_PATH
  16.   
  17.   j:jstools_init            ;# prefs, libraries, bindings...
  18.   
  19.   j:rt:mkabbrevs            ;# e.g. hl for j:rt:hl
  20.   set JDOC_PATH "
  21.     .
  22.     [glob -nocomplain ~/tk/jdoc]
  23.     [glob -nocomplain ~/.tk/jdoc]
  24.     $jstools_library/jdoc
  25.     $tk_library/jdoc
  26.   "
  27. }
  28.  
  29. ##############################################################################
  30. # jdoc:load_topic - find and load a doc file,
  31. #   possibly jumping to a particular point in it
  32. #   the contents of the doc file will set up the Sections menu
  33. ##############################################################################
  34. ### USE OF MOSAIC SHOULD BE A PREFERENCE!
  35.  
  36. proc jdoc:load_topic { topic } {
  37.   global JDOC_PREFS JDOC_PATH
  38.   
  39.   .menu.sections.m delete 0 last
  40.   
  41.   set tmp_list [split $topic "#"]
  42.   set resource [lindex $tmp_list 0]
  43.   set anchor [lindex $tmp_list 1]
  44.   
  45.   # if it's a URL, view it with NCSA Mosaic
  46.   #   (THIS SHOULD BE A PREFERENCE!)
  47.   if {[string match "http:*" $resource] || \
  48.     [string match "ftp:*" $resource] || \
  49.     [string match "*.html" $resource]} {
  50.     wm withdraw .
  51.     exec Mosaic $resource
  52.     exit 0
  53.   }
  54.   
  55.   set FOUND 0
  56.   
  57.   if [string match "*.jdoc" $resource] {
  58.     set filename $resource
  59.   } else {
  60.     set filename $resource.jdoc
  61.   }
  62.   
  63.   ###
  64.   ### THE FOLLOWING NEEDS REWRITTEN FOR SIMPLICITY:
  65.   ###
  66.   if { ! [string match "/*" $filename]} {
  67.     #
  68.     # not absolute path:
  69.     #
  70.     foreach dir $JDOC_PATH {
  71.       if [file exists $dir/$filename] then {
  72.         set FOUND 1
  73.         tkwait visibility .t        ;# unpatched Tk 3.6 bug workaround
  74.         j:tag:restore_text_widget .t $dir/$filename
  75.         .t tag remove sel 1.0 end
  76.         .t mark set insert 1.0
  77.         .t yview 1.0
  78.         .t configure -state disabled
  79.   #      source $dir/$filename        ;# NEED BACKWARDS COMPATIBILITY!
  80.         if {"x$anchor" != "x"} {        ;# jump to anchor given
  81.           jdoc:go_to_anchor $anchor .t
  82.         }
  83.         break
  84.       }
  85.     }
  86.   } else {
  87.     #
  88.     # absolute path:
  89.     #
  90.     if [file exists $filename] then {
  91.       set FOUND 1
  92.       j:tag:restore_text_widget .t $filename
  93.       .t tag remove sel 1.0 end
  94.       .t mark set insert 1.0
  95.       .t yview 1.0
  96.       .t configure -state disabled
  97. #      source $dir/$filename        ;# NEED BACKWARDS COMPATIBILITY!
  98.       if {"x$anchor" != "x"} {        ;# jump to anchor given
  99.         .t yview jdoc:anchorname:${anchor}.first
  100.       }
  101.     }
  102.   }
  103.   
  104.   if {!$FOUND} then {
  105.     .t configure -state normal
  106.     j:rt text .t
  107.     j:rt:hl "Can't find a document called `$resource'."
  108.     j:rt:par
  109.     j:rt:rm "The requested document was not found.  "
  110.     j:rt:rm "It may not have been installed at your site."
  111.     j:rt:done
  112.     .t configure -state disabled
  113.     tkwait window .
  114.     exit 1
  115.   }
  116.   
  117.   .menu.sections.m add command -label "Top" -command {
  118.     .t mark set insert 1.0
  119.     .t yview insert
  120.   }
  121.   .menu.sections.m add separator
  122.   foreach pair [jdoc:find_sections .t] {
  123.     set section [lindex $pair 0]
  124.     set location [lindex $pair 1]
  125.     .menu.sections.m add command -label $section \
  126.       -command ".t mark set insert $location; .t yview insert"
  127.   }
  128.   .menu.sections.m add separator
  129.   .menu.sections.m add command -label "Bottom" -command {
  130.     .t mark set insert end
  131.     .t yview -pickplace insert
  132.   }
  133.   
  134.   wm title . "$resource"
  135.   wm iconname . "$resource"
  136. }
  137.  
  138. ######################################################################
  139. # jdoc:find_sections t - find all level 1 headings in text
  140. ######################################################################
  141.  
  142. proc jdoc:find_sections { t } {
  143.   set ranges [$t tag ranges richtext:font:heading1]
  144.   set sections {}
  145.   
  146.   ;# step through ranges two-at-a-time (start and end)
  147.   while { [llength $ranges] > 0 } {
  148.     set start [lindex $ranges 0]
  149.     set end [lindex $ranges 1]
  150.     set ranges [lreplace $ranges 0 1]    ;# with nothing, ie shift
  151.     set section_name [string trim [$t get $start $end]]
  152.     set section_name [lindex [split $section_name "\n"] 0]
  153.     lappend sections [list $section_name $start]
  154.   }
  155.   return $sections
  156. }
  157.  
  158. ######################################################################
  159. # jdoc:configure_text t - text widget configuration
  160. ######################################################################
  161.  
  162. proc jdoc:configure_text { { t .t } } {
  163.   global JDOC_PREFS
  164.   
  165.   if {$JDOC_PREFS(textwidth) < 20} {set JDOC_PREFS(textwidth) 20}
  166.   if {$JDOC_PREFS(textheight) < 4} {set JDOC_PREFS(textheight) 4}
  167.   
  168.   # fonts:
  169.   j:rt text .t                ;# let j:rt set default font
  170.   j:rt:done
  171.   
  172.   # hypertext styles and bindings:
  173.   $t tag configure jdoc:xref:link -underline 1
  174.   $t tag configure jdoc:xref:manpage -underline 1
  175.   $t tag bind jdoc:xref:link <ButtonRelease-1> \
  176.     {jdoc:x_link %W %x %y}
  177.   $t tag bind jdoc:xref:manpage <ButtonRelease-1> \
  178.     {jdoc:x_manpage %W %x %y}
  179.   $t tag configure jdoc:xref:link -underline 1
  180.   $t tag bind jdoc:xref:manpage <ButtonRelease-1> {jdoc:x_manpage %W %x %y}
  181.   
  182.   #
  183.   # OBSOLETE:
  184.   #
  185.   $t tag configure jdoc:xref:topic -underline 1 \
  186.     -background grey75 -borderwidth 1 -relief raised
  187.   $t tag configure jdoc:xref:section -underline 1 \
  188.     -background grey75 -borderwidth 1 -relief raised
  189. ###  $t tag bind jdoc:xref:topic <ButtonRelease-1> {jdoc:x_topic %W %x %y}
  190. ###  $t tag bind jdoc:xref:section <ButtonRelease-1> {jdoc:x_section %W %x %y}
  191.   
  192.   # other configuration
  193.   catch {.t configure -width $JDOC_PREFS(textwidth)}
  194.   catch {.t configure -height $JDOC_PREFS(textheight)}
  195.   catch {.t configure -background $JDOC_PREFS(textbg)}
  196.   catch {.t configure -foreground $JDOC_PREFS(textfg)}
  197.   catch {.t configure -borderwidth $JDOC_PREFS(textbw)}
  198.   catch {.t configure -selectbackground $JDOC_PREFS(textsb)}
  199.   catch {.t configure -selectforeground $JDOC_PREFS(textsf)}
  200.   catch {.t configure -selectborderwidth $JDOC_PREFS(textsbw)}
  201. }
  202.  
  203. ##############################################################################
  204. # jdoc:first_doc_file - read first doc file from command line
  205. ##############################################################################
  206.  
  207. proc jdoc:first_doc_file {} {
  208.   global argc argv
  209.   
  210.   if {$argc > 1} then {
  211.     wm withdraw .
  212.     j:alert -text \
  213.       "jdoc called with too many arguments.\nUsage: jdoc <topic>"
  214.     exit 1
  215.   }
  216.   
  217.   if {$argc != 1} then {
  218.     update
  219.     jdoc:cmd:load
  220.   } else {
  221.     set topic [lindex $argv 0]
  222.     jdoc:load_topic $topic
  223.   }
  224. }
  225.  
  226.